<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>getSnapShotBeforeUpdate</title>
</head>

<body>
    <div id="app"></div>

    <script src="../../00_课程资料/js@17.0.2/react.development.js"></script>
    <script src="../../00_课程资料/js@17.0.2/react-dom.development.js"></script>
    <script src="../../00_课程资料/js@17.0.2/babel.min.js"></script>

    <script type="text/babel">
        class Count extends React.Component{
            state = {
                count:0
            }
            add = ()=>{
                let {count} = this.state
                count++
                this.setState({
                    count:count
                })
            }
            // 这个函数返回一个快照或者null，返回值交由 componentDidUpdate
            getSnapshotBeforeUpdate(){
                return 'songzx'
            }
            render(){
                return (
                    <div>
                        <h2>和是：{this.state.count}</h2> 
                        <button onClick={this.add}>增加</button>
                    </div>
                )
            }
            // 更新完成函数接收三个参数，分别是上一次传递的props，更新前的state，getSnapshotBeforeUpdate函数返回的快照值
            componentDidUpdate(prevprops,prevstate,snapshot){
                console.log(prevprops);
                console.log(prevstate);
                console.log(snapshot); //=> songzx
            }
        }
        ReactDOM.render(<Count count={9}/>,document.getElementById('app'))

    </script>
</body>

</html>